package com.icepeach.icepeachhomeback.config;


import com.icepeach.icepeachhomeback.exception.ServiceException;
import lombok.extern.log4j.Log4j2;

import java.util.Collection;
import java.util.List;

@Log4j2
public class ServiceAssert {

    /**
     * 判断是否正确,错误抛异常
     * @param expression
     * @param message 异常信息
     * @throws ServiceException
     */
    public static void isTrue(boolean expression, String message) throws ServiceException {
        if (!expression) {
            throw new ServiceException(message);
        }
    }

    /**
     * 是否为null
     * @param object
     * @param message	异常信息
     * @throws ServiceException
     */
    public static void isNull(Object object, String message) throws ServiceException {
        if (object != null) {
            throw new ServiceException(message);
        }
    }

    /**
     * 判断是否正确,错误抛异常
     * @param expression
     * @param message 异常信息
     * @throws ServiceException
     */
    public static void isTrue(boolean expression, String message, String logerrorInfo) throws ServiceException {
        if (!expression) {
            log.error(logerrorInfo);
            throw new ServiceException(message);
        }
    }

    /**
     * 不为null
     * @param object
     * @param message	异常信息
     * @throws ServiceException
     */
    public static void isNotNull(Object object, String message) throws ServiceException {
        if (object == null) {
            throw new ServiceException(message);
        }
    }

    /**
     * 集合是否为空
     * @param list
     * @param message	异常信息
     * @throws ServiceException
     */
    @Deprecated
    public static void arrayHasValue(List<? extends Object> list, String message) throws ServiceException {
        if (list.isEmpty()) {
            throw new ServiceException(message);
        }
    }

    /**
     * 集合是否为空
     * @param list
     * @param message	异常信息
     * @throws ServiceException
     */
    public static void collectionHasValue(Collection<? extends Object> collection, String message) throws ServiceException {
        if (collection.isEmpty()) {
            throw new ServiceException(message);
        }
    }

    /**
     * 是否包含某字符串
     * @param text
     * @param message	异常信息
     * @throws ServiceException
     */
    public static void hasText(String text, String message) throws ServiceException {
        if (!hasText(text)) {
            throw new ServiceException(message);
        }
    }
    /**
     * 是否包含某元素
     * @param target	目标元素
     * @param resource	集合
     * @param message	异常信息
     * @throws ServiceException
     */
    public static void arrayContains(String target, List<String> resource, String message) throws ServiceException{
        if(!resource.contains(target)) {
            throw new ServiceException(message);
        }
    }
    /**
     * 字符串是否为空
     * @param str
     * @return
     */
    private static boolean hasText(String str) {
        return (str != null && !str.isEmpty() && !str.trim().isEmpty());
    }
}

